<?php

namespace App\Http\Controllers;

use App\Models\AdminMenusModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class MenuController extends BaseController
{

    protected $rule = [
        'type' => 'required|integer',
        'pid' => 'required|integer',
        'name' => 'required',
        'rule' => 'required|regex:/^[a-zA-Z][a-zA-Z0-9\.]{0,50}$/',
    ];

    protected $message = [];

    public function __construct()
    {
        $this->message = [
            'type.required' => trans('menu.type_required'),
            'pid.required' => trans('menu.pid_required'),
            'name.required' => trans('menu.name_required'),
            'rule.required' => trans('menu.rule_required'),
            'type.interger' => trans('menu.type_is_not_interger'),
            'pid.interger' => trans('menu.pid_is_not_interger'),
            'rule.regex' => trans('menu.rule_regex')
        ];
    }

    /**
     * 菜单列表页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $recursion_menus = AdminMenusModel::getAllAdminMenusForPageMenu();
        return view('menu.index', [
            "menus" => $recursion_menus,
            'types' => AdminMenusModel::getAllTypes(),
        ]);
    }

    /**
     * 展示新增 修改 添加菜单页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Request $request)
    {
        $types = AdminMenusModel::getAllTypes();
        $is_pid = $request->has('pid') ? true : false;
        $recursion_menus = AdminMenusModel::getAllAdminMenusForPageMenu(['is_not_btn' => 3]);
        if ($request->has('id')) {
            $id = $request->input('id');
            $menu = AdminMenusModel::getAdminMenusData(['id' => $id]);
            return view('menu.show', [
                'types' => $types,
                'menus' => $recursion_menus,
                'menu_show' => $menu,
                'is_pid' => $is_pid
            ]);
        }
        return view('menu.show', [
            'types' => $types,
            'menus' => $recursion_menus,
            'is_pid' => $is_pid
        ]);
    }

    /**
     * 选择图标页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function icon()
    {
        return view("menu.icon");
    }

    /**
     * 添加菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        if (!$request->ajax()) {
            return $this->_failed(trans('common.illegal_request'));
        }
        $data = $request->input();
        $validator = Validator::make($data, $this->rule, $this->message);
        if (!$validator->passes()) {
            return $this->_failed($validator->errors()->first());
        }

        $pidType = AdminMenusModel::TOP;
        $pmenu = AdminMenusModel::getAdminMenusData(['id' => $data['pid']]);
        if ($data['pid'] > 0 && !is_null($pmenu)) {
            $pidType = $pmenu->type;
        }
        $mes = $this->judgeMenu($data['type'], $pidType);
        if ($mes !== true) {
            return $this->_failed($mes);
        }
        $data['pid'] = $data['pid'] == -1 ? 0 : $data['pid'];

        if (AdminMenusModel::getAdminMenusExists(['name' => $data['name'], 'pid' => $data['pid']])) {
            return $this->_failed(trans('menu.same_pid_repeat'));
        }
        if (AdminMenusModel::getAdminMenusExists(['rule' => $data['rule']])) {
            return $this->_failed(trans('menu.rule_repeat'));
        }
        if (!AdminMenusModel::insertAdminRoles(array_filter($data))) {
            return $this->_failed(trans('common.insert_failed'));
        }
        return $this->_success();
    }

    private function judgeMenu($type, $pidType)
    {
        //顶部菜单应该对应模块
        if ($pidType == AdminMenusModel::TOP && in_array($type, [AdminMenusModel::CHILDMODULE, AdminMenusModel::MENU, AdminMenusModel::BUTTON])) {
            return trans("menu.top_menu_module");
        }
        //模块应该对应子模块
        if ($pidType == AdminMenusModel::MODULE && in_array($type, [AdminMenusModel::MODULE, AdminMenusModel::MENU, AdminMenusModel::BUTTON])) {
            return trans("menu.module_menu_childmodule");
        }
        //子模块对应菜单
        if ($pidType == AdminMenusModel::CHILDMODULE && in_array($type, [AdminMenusModel::MODULE, AdminMenusModel::CHILDMODULE, AdminMenusModel::BUTTON])) {
            return trans("menu.childmodule_menu_menu");
        }
        //菜单对应按钮
        if ($pidType == AdminMenusModel::MENU && in_array($type, [AdminMenusModel::MODULE, AdminMenusModel::CHILDMODULE, AdminMenusModel::MENU])) {
            return trans("menu.menu_menu_btn");
        }
        return true;
    }


    /**
     * 编辑菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        if (!$request->ajax()) {
            return $this->_failed(trans('common.illegal_request'));
        }
        $data = $request->input();
        $validator = Validator::make($data, $this->rule, $this->message);
        if (!$validator->passes()) {
            return $this->_failed($validator->errors()->first());
        }

        $pidType = AdminMenusModel::TOP;
        $pmenu = AdminMenusModel::getAdminMenusData(['id' => $data['pid']]);
        if ($data['pid'] > 0 && !is_null($pmenu)) {
            $pidType = $pmenu->type;
        }
        $mes = $this->judgeMenu($data['type'], $pidType);
        if ($mes !== true) {
            return $this->_failed($mes);
        }
        $data['pid'] = $data['pid'] == -1 ? 0 : $data['pid'];

        $menu = AdminMenusModel::getAdminMenusData(['id' => $data['id']]);
        if ($menu->name != $data['name']) {
            if (AdminMenusModel::getAdminMenusExists(['name' => $data['name'], 'pid' => $data['pid']])) {
                return $this->_failed(trans('menus.same_pid_repeat'));
            }
        }
        if ($menu->rule != $data['rule']) {
            if (AdminMenusModel::getAdminMenusExists(['rule' => $data['rule']])) {
                return $this->_failed(trans('menus.rule_repeat'));
            }
        }
        $update = array_merge(array_filter($data), ['update_time' => time()]);
        unset($update['id']);
        if (!AdminMenusModel::updateAdminMenus(['id' => $data['id']], $update)) {
            return $this->_failed(trans('common.update_failed'));
        }
        return $this->_success();
    }


    /**
     * 删除菜单不支持删除菜单下所有子菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        if (!$request->ajax()) {
            return $this->_failed(trans('common.illegal_request'));
        }
        $id = $request->input('id');
        $menu = AdminMenusModel::getAdminMenusExists(['id' => $id]);
        if (!$menu) {
            return $this->_failed(trans('menu.not_exists_menu'));
        }
        $cmenu = AdminMenusModel::getAdminMenusExists(['pid' => $id]);
        if ($cmenu) {
            return $this->_failed(trans('menu.please_del_child_menus'));
        }
        if (!AdminMenusModel::deleteAdminMenus(['id' => $id])) {
            return $this->_failed(trans('common.del_failed'));
        }
        return $this->_success();
    }

    /**
     * 显示和隐藏菜单和子菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function disable(Request $request)
    {
        if (!$request->ajax()) {
            return $this->_failed(trans('common.illegal_request'));
        }
        $id = intval($request->input('id'));
        $status = intval($request->input('status'));
        $menuIds = AdminMenusModel::getAllChildMenusIds($id);
        if (empty($menuIds)) {
            return $this->_failed(trans('menu.not_exists_menu_child'));
        }
        $update ['status'] = AdminMenusModel::MENUSHOW;
        if ($status == AdminMenusModel::MENUSHOW) {
            $update['status'] = AdminMenusModel::MENUHIDE;
        }
        if (!AdminMenusModel::updateAdminMenus(['id' => $menuIds], $update)) {
            return $this->_failed(trans('common.operation_failed'));
        }
        return $this->_success();
    }
}
